<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title>Arrays in Tcl</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="Tcl, learn Tcl, arrays, tutorial, programming language, Linux">
<meta name="description" content="This part of the Tcl tutorial covers Tcl arrays.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>


<div class="content">

<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Arrays in Tcl</h1>

<p>
In this part of the Tcl programming tutorial, we will cover arrays. 
We will initiate arrays and read data from them.
</p>

<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* LargeSquare */
google_ad_slot = "5070696322";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

<p>
An array in Tcl is a data structure, which binds a key with a value. 
A key and a value can be any Tcl string. 
</p>


<pre class="code">
#!/usr/bin/tclsh

set names(1) Jane
set names(2) Tom
set names(3) Elisabeth
set names(4) Robert
set names(5) Julia
set names(6) Victoria

puts [array exists names]
puts [array size names]

puts $names(1)
puts $names(2)
puts $names(6)
</pre>

<p>
We create a names array. The numbers are keys and the names are
values of the array. 
</p>

<pre class="explanation">
set names(1) Jane
</pre>

<p>
In this line we set a value Jane to the array key 1. We can later
refer to the value by the key. 
</p>

<pre class="explanation">
puts [array exists names]
</pre>

<p>
The <code>array exists</code> command determines, whether the array
is created. Returns 1 if true, 0 otherwise. 
</p>

<pre class="explanation">
puts [array size names]
</pre>

<p>
We get the size of the array with the <code>array size</code> command. 
</p>

<pre class="explanation">
puts $names(1)
</pre>

<p>
We access a value from the array by its key. 
</p>

<pre>
$ ./names.tcl
1
6
Jane
Tom
Victoria
</pre>

<p>
Output. 
</p>

<hr class="btm">

<p>
Arrays can be initiated with the <code>array set</code>
command. 
</p>

<pre class="code">
#!/usr/bin/tclsh

array set days {
    1 Monday
    2 Tuesday
    3 Wednesday
    4 Thursday
    5 Friday
    6 Saturday
    7 Sunday
}

foreach {n day} [array get days] {

    puts "$n -> $day"
}
</pre>

<p>
We create a day array. It has 7 keys and values. 
</p>

<pre class="explanation">
foreach {n day} [array get days] {
</pre>

<p>
The <code>array get</code> command returns a list
of key, value elements, which can be iterated with the
<code>foreach</code> command. 
</p>

<pre>
$ ./days.tcl
4 -> Thursday
5 -> Friday
1 -> Monday
6 -> Saturday
2 -> Tuesday
7 -> Sunday
3 -> Wednesday
</pre>

<p>
Output. 
</p>

<hr class="btm">

<p>
We show another way to traverse an array in Tcl.
</p>

<pre class="code">
#!/usr/bin/tclsh

array set nums { a 1 b 2 c 3 d 4 e 5 }

puts [array names nums]

foreach n [array names nums] {

    puts $nums($n)
}
</pre>

<p>
The script uses the <code>array names</code> command
to traverse the array. 
</p>

<pre class="explanation">
array set nums { a 1 b 2 c 3 d 4 e 5 }
</pre>

<p>
We define a simple array.
</p>

<pre class="explanation">
puts [array names nums]
</pre>

<p>
The <code>array names</code> returns a list containing 
the names of all of the elements in the array. 
</p>

<pre class="explanation">
foreach n [array names nums] {

    puts $nums($n)
}
</pre>

<p>
We use the keys to get the values. 
</p>

<pre>
$ ./getnames.tcl 
d e a b c
4
5
1
2
3
</pre>

<p>
Output. 
</p>

<hr class="btm">

<p>
In the last example of this chapter, we will show how
to remove elements from the array.
</p>

<pre class="code">
#!/usr/bin/tclsh

set names(1) Jane
set names(2) Tom
set names(3) Elisabeth
set names(4) Robert
set names(5) Julia
set names(6) Victoria

puts [array size names]
unset names(1)
unset names(2)

puts [array size names]
</pre>

<p>
We create a names array. We use the <code>unset</code> command to 
remove items from the array. We check the size of the array before and
after we remove the two items. 
</p>

<pre class="explanation">
set names(1) Jane
</pre>

<p>
The <code>set</code> command is used to create an item in the
array. 
</p>

<pre class="explanation">
unset names(1)
</pre>

<p>
We use the <code>unset</code> command to remove an element
with key 1 from the array. 
</p>

<p>
In this part of the Tcl tutorial, we worked with arrays. 
</p>


<br>
<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 
<br>

<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified April 14, 2011  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

